﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using Common;
using XmlReader = System.Xml.XmlReader;

namespace ConsoleApplication1
{
    internal class Program
    {
        private const string XmlFileName = "PRItems.xml";
        private const string SchemaFileName = @"Schema.xsd";

        private static void Main(string[] args)
        {

            //if (ValidateXml(XmlFileName, SchemaFileName))
            //{
            //    Console.WriteLine("Validate Success!");
            //    Console.ReadLine();
            //}
            //else
            //{
            //    Console.WriteLine("Validate Fail!");
            //    Console.ReadLine();
            //}

            using (SqlConnection con = DBHelper.GetSqlConnection())
            {
                con.Open();
                SqlTransaction tran = con.BeginTransaction();
                try
                {
                    test(con, tran);
                    tran.Commit();
                }
                catch (SqlException ex1)
                {
                    Logger.WriteLog("执行resource事务数据库异常", ex1);
                    try
                    {
                        tran.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        Logger.WriteLog("执行resource事务回滚异常", ex2);
                    }
                }
                catch (Exception ex1)
                {
                    Logger.WriteLog("执行resource事务异常", ex1);
                    try
                    {
                        tran.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        Logger.WriteLog("执行resource事务回滚异常", ex2);
                    }
                }
                finally
                {
                    con.Close();
                }
            }


        }

        public static void test(SqlConnection connection, SqlTransaction transaction)
        {
            SqlConnection conn = connection;
            SqlTransaction tran = transaction;
        }


        static bool ValidateXml(string xmlFile, string schemaFile)
        {
            bool isValid = true;
            try
            {
                XmlSchemaSet schemaSet = new XmlSchemaSet();
                schemaSet.Add(null, schemaFile);
                XmlReaderSettings readerSetting = new XmlReaderSettings();
                readerSetting.ValidationType = ValidationType.Schema;
                readerSetting.Schemas = schemaSet;

                using (XmlReader xmlReader = XmlReader.Create(xmlFile, readerSetting))
                {
                    while (xmlReader.Read())
                    { }
                }
            }
            catch (Exception ex)
            {
                isValid = false;
                Console.WriteLine("Validate Failure! Error: {0}", ex.Message);
            }

            return isValid;
        }



    }
}
